//###############
// FILE : StatusController.java
// WRITER : Lior Zimmerman liorz06 066462300
// WRITER : Ohad Dan ohad_dan 036767960
// EXERCISE : oop ex3 2008
// DESCRIPTION:
// Abstract class for controllers.
//###############
package oop.ex3.fileSearchController;



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import oop.ex3.fileSearchController.testableGui.FilterLocationEnum;
import oop.ex3.fileSearchController.testableGui.OutputDestinationEnum;
import oop.ex3.fileSearchController.testableGui.TestableGUI;
import oop.ex3.fileSearchView.GUIView;

/**
 * This class is the father class of controllers.
 * @author liorz06
 *@author ohad_dan
 */
public abstract class Controller implements TestableGUI, ActionListener,
                                    DocumentListener{
    
    //********************************************************************
    //  Data members :                                                   *
    //********************************************************************
    //pointer for the gui view
    protected GUIView _gui;
    //********************************************************************
    //  Methods :                                                        *
    //********************************************************************  
    /**
     * Construct a new controller.
     * @param gui The frame in which the controller is contained.
     */
    public Controller(GUIView gui) {
        _gui = gui;
    }
    /**
     * resets the forms
     */
    public void clickReset() {
        ResetAction reseter = new ResetAction(_gui);
        reseter.doAction();
        
    }
    /**
     * runs the search
     */
    public void clickRun() {
        SearchAction searcher = new SearchAction(_gui);
        searcher.doAction();
        
    }
    /**
     * returns the filter expression
     * @return filterExp
     */
    public String getFilterExpression() {
        return _gui.getSearchView().txtFilterExpGetText();
    }
    /**
     * returns the filter file path
     * @return filterPath
     */
    public String getFilterFile() {
        return _gui.getSearchView().txtFilterFileGetText();
    }
    /**
     * returns the location of the filter (file or text)
     * @return location
     */
    public FilterLocationEnum getFilterLocation() {
        if ( _gui.getSearchView().rdoFilterFromFileSelected() ){
            return FilterLocationEnum.FILE;
        }
        return FilterLocationEnum.TEXT;
    }
    /**
     * returns the log text.
     * @return logText
     */
    public String getLog() {
        return _gui.getLogView().getLogTxt();
    }
    /**
     * returns the output destination (file, screen, file and screen)
     * @return destionation
     */
    public OutputDestinationEnum getOutputDestination() {
        if ( _gui.getSetupView().rdoToFileSelected() ) {
            return OutputDestinationEnum.FILE;
        }else if( _gui.getSetupView().rdoToScreenSelected() ){
            return OutputDestinationEnum.SCREEN;
        }
        return OutputDestinationEnum.FILE_AND_SCREEN;
    }
    /**
     * returns the status panel text
     * @return statusText
     */
    public String getStatusPanelText() {
        return _gui.getStatus();
    }
    /**
     * returns the working directory
     * @return workDir
     */
    public String getWorkingDirectory() {
        return _gui.getSearchView().txtWorkDirGetText();
    }
    /**
     * sets the filter expression
     * @param exp
     * 
     */
    public void setFilterExpression(String exp) {
        _gui.getSearchView().txtFilterExpSetText(exp);
        
    }
    /**
     * sets the filter file path
     * @param fileName the filename
     */
    public void setFilterFile(String fileName) {
        _gui.getSearchView().txtFilterFileSetText(fileName);
    }
    /**
     * sets the filter location
     * @param location the filter location
     */
    public void setFilterLocation(FilterLocationEnum location) {
        if ( location == null ) {return;}
        if ( location == FilterLocationEnum.FILE ) {
            _gui.getSearchView().rdoSetSelectedFilterFromFile(true);
            return;
        }
        _gui.getSearchView().rdoSetSelectedFilterFromText(true);
    }
    /**
     * sets the output destination
     * @param outputDest
     */
    public void setOutputDestination(OutputDestinationEnum outputDest) {
        if ( outputDest == null ) { return; }
        if ( outputDest == OutputDestinationEnum.FILE ) {
            _gui.getSetupView().rdoSetSelectedToFile(true);
            return;
        }else if ( outputDest == OutputDestinationEnum.SCREEN ) {
            _gui.getSetupView().rdoSetSelectedToScreen(true);
            return;
        }
        _gui.getSetupView().rdoSetSelectedToFileScreen(true);
    }
    /**
     * sets the output file path
     * @param fileName
     */
    public void setOutputFile(String fileName) {
        _gui.getSetupView().setTextOutputFile(fileName);
    }
    /**
     * sets the working directory
     * @param dirName
     */
    public void setWorkingDirectory(String dirName) {
        _gui.getSearchView().txtWorkDirSetText(dirName);      
    }
    /**
     * performes the action
     * @param e the event
     */
    public void actionPerformed( ActionEvent e ){
        doAction();
    }
    /**
     * performes the action
     * @param e the event
     */
    public void changedUpdate(DocumentEvent e){
        doAction();
    }
    /**
     * performes the action
     * @param e the event
     */
    public void insertUpdate(DocumentEvent e){
        doAction();
    }
    /**
     * performes the action
     * @param e the event
     */
    public void removeUpdate(DocumentEvent e){
        doAction();
    }
    /**
     * performes the specific action for a filter
     */
    public abstract void doAction();

}
